//
//  CopyPathView.swift
//  L02DrawShapes
//
//  Created by liy on 2024/12/25.
//

import UIKit

class CopyPathView: UIView {
  override func draw(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    let rect = CGRectMake(150, 150, 200, 200)
    context?.beginPath()
    context?.addRect(rect)
    guard let copiedPath = (context!.path)!.copy() else { return }
    context?.setFillColor(UIColor.red.cgColor)
    context?.fillPath()
    context?.addPath(copiedPath)
    context?.setLineWidth(15)
    context?.setStrokeColor(UIColor.blue.cgColor)
    context?.strokePath()
  }
}

#if DEBUG

  import SwiftUI

  struct CopyPathViewRepresentable: UIViewRepresentable {
    typealias UIViewType = CopyPathView

    func makeUIView(context: Context) -> UIViewType {
      let v = CopyPathView()
      v.backgroundColor = .init(white: 0.7, alpha: 1)
      return v
    }

    func updateUIView(_ uiView: UIViewType, context: Context) {
    }
  }

  struct CopyPathView_Previews: PreviewProvider {
    static var previews: some View {
      CopyPathViewRepresentable()
    }
  }

#endif
